// const BASE_URl_TEST = 'http://192.168.20.213:8989';
const BASE_URl_TEST = 'http://localhost:8989';
/*
以前接口规范:
  get: 获取数据，删除数据
  post ： 修改数据，修改状态，添加数据

rest接口规范 :使用接口的请求方式+请求的资源路径 做到接口请求的唯一性
  get:获取数据
  post:添加数据
  put :修改数据
  delete: 删除数据
  patch ：修改单个字段
*/

//使用GET获取角色分页列表
export const getRolesPage = async () => {
  try {
    const res = await fetch(BASE_URl_TEST + '/roles/page?pageNum=1&pageSize=100');
    return res.json();
  } catch (error) {
    console.log(error + 'error');
  }
};

//使用POST增加角色
export const postRoles = async () => {
  const myHeaders = new Headers();
  myHeaders.append('Content-Type', 'application/json');
  const newRole = {
    name: '张振中111',
    code: '张振中111',
    sort: 51,        //不冲突即可
    status: 1,      //0/1
    dataScope: 1,   //0-3
  };
  const resquestOptions = {
    method: 'POST',
    headers: myHeaders,
    body: JSON.stringify(newRole),
  };
  const res = await fetch(BASE_URl_TEST + '/roles', resquestOptions);
  if (res.ok) {
    return res.json();
  }
};

//使用PUT修改角色信息  id
export const putRolesByRoleID = () => {
  const myHeaders = new Headers();
  myHeaders.append('Content-Type', 'application/json'); //请求头  省略掉了token

  const raw = {
    name: '张廷中2211111',
    code: '张廷中2211111',
    sort: 222,
    status: 1,
    dataScope: 3,
  };

  const resquestOptions = {
    method: 'PUT',
    headers: myHeaders,
    body: JSON.stringify(raw),
  };

  fetch(BASE_URl_TEST + '/roles/110043', resquestOptions)
    .then((response) => response.text())
    .then((result) => console.log(result))
    .then((error) => console.log(error + 'error'));
};

//使用DELETE删除角色信息
export const deleteRolesByids = () => {
  const resquestOptions = {
    method: 'DELETE',
  };

  fetch(BASE_URl_TEST + '/roles/23482', resquestOptions)
    .then((response) => response.text())
    .then((result) => console.log(result))
    .then((error) => console.log(error + 'error'));
};

//使用patch修改角色单个字段
export const patchRolesByids = () => {
  const resquestOptions = {
    method: 'PATCH',
  };

  fetch(BASE_URl_TEST + '/roles/110040/status?status=2', resquestOptions)
    .then((response) => response.text())
    .then((result) => console.log(result))
    .then((error) => console.log(error + 'error'));
};
